package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;
import android.util.Log;

import com.elinkway.infinitemovies.bean.AdDataBean;
import com.elinkway.infinitemovies.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 广告解析
 * 
 * @author wangyemin
 * 
 */
public class AdDataParser extends LVideoMobileParser<AdDataBean> {

	public static String TAG = "adData";
	private String PARSER_KEY;// 在解析前先设置该字段

	public AdDataParser(String pageFrom) {
		PARSER_KEY = pageFrom;
	}

	public static String STATUS_CODE = "200";
	public static String STATUS = "status";
	public static String CITY = "city";

	public static String IMAGESIZE = "600*100"; // 可能变化
	public static String TYPE = "type";
	public static String SHOWTYPE = "showType";
	public static String ADID = "adId";
	public static String TITLE = "title";
	public static String DESC = "desc";

	public static String HOME = "home"; // 首页两张ad图
	public static String HOME_PAGE_BANNER = "home_page_banner"; // 首页banner图
	public static String HOME_POST = "home_post"; // 首页浮层图
	public static String LAYER_IMAGESIZE = "100*100";

	@Override
	public AdDataBean parse(JSONObject data) throws Exception {
		AdDataBean adData = new AdDataBean();
		Log.e(TAG, "pageFrom is " + PARSER_KEY);
		if (data != null && data.has(STATUS)
				&& data.optString(STATUS).equalsIgnoreCase(STATUS_CODE)
				&& !TextUtils.isEmpty(PARSER_KEY)) {
			adData.setCity(data.optString(CITY));
			// 首页逻辑单独处理
			if (PARSER_KEY.equalsIgnoreCase(HOME)) {
				JSONObject focusObj = data.optJSONObject("home_page_focus");
				if(focusObj != null){
					adData.setFocusAdId(focusObj.optString("adId"));
					adData.setFocusType(focusObj.optString("type"));
					adData.setFocusAdTitle(focusObj.optString(TITLE));
					JSONArray adArr = focusObj.optJSONArray("640*320");
					if (adArr != null && adArr.length() > 0) {
						adData.setFocusAdImage(adArr.optString(0));
						adData.setFocusAdUrl(adArr.optString(1));
					}
					String ext = focusObj.optString("ext");
					try {
						adData.setFocusAdIndex(Integer.parseInt(ext));
					}catch (Exception e){
						adData.setFocusAdIndex(2);
					}
				}

				// 首页banner图数据解析
				if (data.has(HOME_PAGE_BANNER)) {
					JSONObject bannerNameObj = data
							.getJSONObject(HOME_PAGE_BANNER);
					if (bannerNameObj.has(IMAGESIZE)) {
						JSONArray adArr = bannerNameObj.optJSONArray(IMAGESIZE);
						if (adArr != null && adArr.length() > 0) {
							adData.setAdImage(adArr.optString(0));
							adData.setAdUrl(adArr.optString(1));
						}
					}
					if (bannerNameObj.has(TYPE)) {
						adData.setAdType(bannerNameObj.optString(TYPE));
					}
					if (bannerNameObj.has(ADID)) {
						adData.setAdId(bannerNameObj.optString(ADID));
					}
					adData.setAdTitle(bannerNameObj.optString(TITLE));
					adData.setAdDesc(bannerNameObj.optString(DESC));
				}
				// 首页浮层图数据解析
				if (data.has(HOME_POST)) {
					JSONObject bannerNameObj = data.getJSONObject(HOME_POST);
					if (bannerNameObj.has(LAYER_IMAGESIZE)) {
						JSONArray adArr = bannerNameObj
								.getJSONArray(LAYER_IMAGESIZE);
						if (adArr != null && adArr.length() > 0) {
							adData.setLayerAdImage(adArr.getString(0));
							adData.setLayerAdUrl(adArr.getString(1));
						}
					}
					if (bannerNameObj.has(TYPE)) {
						adData.setLayerAdType(bannerNameObj.optString(TYPE));
					}
				}
			}
			// 其他页面
			if (data.has(PARSER_KEY)) {
				JSONObject bannerNameObj = data.optJSONObject(PARSER_KEY);
				if (bannerNameObj.has(IMAGESIZE)) {
					JSONArray adArr = bannerNameObj.optJSONArray(IMAGESIZE);
					if (adArr != null && adArr.length() > 0) {
						adData.setAdImage(adArr.getString(0));
						adData.setAdUrl(adArr.getString(1));
					}
				}
				if (bannerNameObj.has(TYPE)) { 
					adData.setAdType(bannerNameObj.optString(TYPE));
				}
				if (bannerNameObj.has(ADID)) {
					LogUtils.e("", "ADID is " + bannerNameObj.optString(ADID));
					adData.setAdId(bannerNameObj.optString(ADID));
					adData.setAdShowType(bannerNameObj.optString(SHOWTYPE));
				}
				adData.setAdTitle(bannerNameObj.optString(TITLE));
				adData.setAdDesc(bannerNameObj.optString(DESC));
			}
		}
		return adData;
	}
}
